package com.yeyaomai.dksns.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisUtil {
	@Resource
	private JedisPool jedisPool;

	public Set<String> keys(final String pattern) {

		Jedis jedis = jedisPool.getResource();

		try {
			return jedis.keys(pattern);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			release(jedis);
		}
	}

	public Map<String, String> hgetall(final String key) {
		Jedis jedis = jedisPool.getResource();
		try {
			return jedis.hgetAll(key);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			release(jedis);
		}
	}
	
	public long scard(final String key){
		Jedis jedis=jedisPool.getResource();
		try {
			return jedis.scard(key);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			release(jedis);
		}
	}
	
	
	private void release(Jedis jedis) {
		if (null != jedisPool && null != jedis) {
			jedisPool.returnResource(jedis);
		}
	}

	public static void main(String[] args) {
//		Jedis jedis=new jedisc
		ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context.xml");
		JedisPool pool=(JedisPool) ctx.getBean("jedisPool");
		Jedis jedis = pool.getResource();
		
//		jedis.lpush("zuoshuai", "abc");
//		jedis.lpush("zuoshuai", "123");
//		jedis.lpush("zuoshuai", "fsld");
//		List<String> list=jedis.lrange("zuoshuai", 0, -1);
//		
//		for(String value:list){
//			System.out.println(value);
//		}
		//返回key是这个类型的左右key
//		Set<String> keys=jedis.keys("zuo*");
//		for(String key:keys){
//			System.out.println(key);
//		}
		 //返回所有set为key的集合
		//添加set用sadd函数
		Set<String> keys=jedis.smembers("zuoset");
		for(String key:keys){
			System.out.println(key);
		}
		
		
		
	}
}
